//
//  CityLocationView.m
//
//
//  Created by gynjobs on 2017/8/14.
//  Copyright © 2017年 gynjobs. All rights reserved.
//

#import "CityLocationView.h"

@interface CityLocationView()
@property(strong,nonatomic)UIView *headerBgView;
@property(strong,nonatomic)UIView *headerVLine;
@property(strong,nonatomic)UILabel *headerTitleLabel;
@property(strong,nonatomic)NSMutableArray *btnArray;
@end

@implementation CityLocationView

-(instancetype)init{
    
    if(self = [super init]){
        
        
        self.backgroundColor = [UIColor colorWithString:UI_COLOR_MAIN_BG_1];
        self.searchBarView = [[SearchBarView alloc] init];
        self.searchBarView.backgroundColor = [UIColor whiteColor];
        
        self.cityTableView = [[UITableView alloc] init];
        self.searchBarView.contentTableView = self.cityTableView;
        
        
        [self addSubview:self.searchBarView];
        [self addSubview:self.searchBarView.aTableView];
        [self addSubview:self.cityTableView];
        [self updateConstraintsIfNeeded];
    }
    
    return self;
    
}




-(void)updateConstraints{
    [super updateConstraints];
    
    [self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.top.equalTo(self.mas_top);
        make.height.mas_equalTo(60);
        
    }];
    
    [self.searchBarView.aTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.searchBarView.mas_bottom);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    [self.cityTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.searchBarView.mas_bottom).offset(15);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom);
        
    }];
    
    
}

-(void)setCallBackData:(id)hotData withDataSource:(id)dataSource{
    
    self.cityTableView.tableHeaderView = [self genTableViewHeaderView:hotData];
    self.searchBarView.dataSource = dataSource;
    
}

-(id)genTableViewHeaderView:(id)data{
    
    
    self.btnArray = [NSMutableArray arrayWithCapacity:3];
    self.headerBgView = [[UIView alloc] init];
    [self.headerBgView setFrame:CGRectMake(0, 0, 0, 50)];
    
    self.headerBgView.backgroundColor = [UIColor whiteColor];
    
    
    self.headerVLine = [[UIView alloc] init];
    self.headerVLine.backgroundColor = [UIColor colorWithString:UI_COLOR_SPLIT_LINE_1];
    self.headerTitleLabel = [[UILabel alloc] init];
    self.headerTitleLabel.textColor = [UIColor colorWithString:UI_COLOR_COMMON_TEXT_COLOR_1];
    self.headerTitleLabel.font = [UIFont systemFontOfSize:UI_FOND_TAB_CREDIT_CELL_MAIN_1];
    self.headerTitleLabel.text = @"热门城市";
    
    [self.headerBgView addSubview:self.headerTitleLabel];
    [self.headerBgView addSubview:self.headerVLine];
    [self updateHeaderTitle];
    
    if([data count] > 0 ){
        
        NSInteger count = [data count];
        
        NSInteger layer = (count % 3) == 0 ? count / 3 : count/3 + 1;
        NSInteger height = layer * 39 + (layer -1) * 15 + 15 * 2;
        [self.headerBgView setFrame:CGRectMake(0, 0, 0, height + 50)];
        
        for (NSInteger i = 0; i < [data count]; i ++) {
            
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
            [btn setTitleColor:[UIColor colorWithString:@"465e6b"] forState:UIControlStateNormal];
            btn.layer.cornerRadius = 3.0;
            btn.layer.borderWidth = 1.0;
            btn.layer.borderColor = [UIColor colorWithString:@"#e7e7e7"].CGColor;
            [btn addTarget:self action:@selector(hotBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.headerBgView addSubview:btn];
            
            NSString * title = [[data objectAtIndex:i] valueForKey:@"zone_name"];
            [btn setTitle:title forState:UIControlStateNormal];
            [self.btnArray addObject:btn];
        }
        
        [self updateHeaderBtns];
        
    }
    
    return self.headerBgView;
}

-(void)updateHeaderTitle{
    
    [self.headerTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.headerBgView.mas_left).with.offset(LEFT_RIGHT_SPACE);
        make.top.equalTo(self.headerBgView.mas_top).with.offset(18);
        
    }];
    
    [self.headerVLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.headerBgView.mas_top).with.offset(50);
        make.left.equalTo(self.headerBgView);
        make.right.equalTo(self.headerBgView);
        make.height.mas_equalTo(0.5);
        
    }];
    
    
}

-(void)updateHeaderBtns{
    
    
    NSInteger btnWidth = [self genBtnWidth:3 withLeading:15 withSpace:18];
    for (NSInteger i = 0; i < [self.btnArray count]; i ++ ) {
        
        // constraint
        
        UIButton * btn = [self.btnArray objectAtIndex:i];
        UIView * leftPreView = nil;
        UIView * topPreView = nil;
        if(i % 3 == 0){
            
            leftPreView = self.headerBgView;
            
        }else{
            
            leftPreView = [self.btnArray objectAtIndex:i-1];
        }
        
        NSInteger j = i - 3 ;
        if(j < 0){
            
            topPreView = self.headerVLine;
            
        }else {
            
            topPreView = [self.btnArray objectAtIndex: i - 3];
        }
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if(leftPreView == self.headerBgView){
                
                make.left.equalTo(leftPreView.mas_left).with.offset(15);
                
            }else{
                
                make.left.equalTo(leftPreView.mas_right).with.offset(18);
            }
            
            make.top.equalTo(topPreView.mas_bottom).with.offset(15);
            make.width.mas_equalTo(btnWidth);
            make.height.mas_equalTo(39);
            
        }];
        
        
    }
    
}


-(NSInteger) genBtnWidth:(NSInteger) columnNum  withLeading:(NSInteger) leading  withSpace:(NSInteger) space{
    
    if(columnNum <= 3){
        
        columnNum = 3;
    }
    
    NSInteger width = [UIScreen mainScreen].bounds.size.width - 10;
    NSInteger btnWidth = (width - leading * 2 - (columnNum -1) * space)/columnNum;
    return btnWidth;
    
}

-(void)hotBtnPressed:(id)sender{
    
    NSInteger index = [self.btnArray indexOfObject:sender];
    if(self.cityDelegate && [self.cityDelegate respondsToSelector:@selector(cityLocationHotIndex:)]){
        
        [self.cityDelegate cityLocationHotIndex:index];
    }
    
}


@end
